package com.awenhui.demo.config;

import com.awenhui.demo.module.security.CustomAuthenticationFailureHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.config.annotation.authentication.builders.AuthenticationManagerBuilder;
import org.springframework.security.config.annotation.method.configuration.EnableGlobalMethodSecurity;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.AuthenticationSuccessHandler;

/**
 * @author yuxf
 * @version 1.0
 * @date 2020/10/26 8:54
 */
@EnableWebSecurity
@EnableGlobalMethodSecurity(prePostEnabled = true)
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    public UserDetailsService userDetailsServiceImpl;

    @Autowired
    public AuthenticationSuccessHandler customAuthenticationSucessHandler;

    @Autowired
    private CustomAuthenticationFailureHandler authenticationFailureHandler;

    @Override
    protected void configure(HttpSecurity http) throws Exception {

        http.csrf().disable();//此处配置禁用，否则post请求会被拦截

        http.authorizeRequests().antMatchers("/login","/doLogin").permitAll()
                .antMatchers("/vcodeLogin").permitAll()
                .antMatchers("/demo/**","/test/**","/shiro/**").permitAll()
                // swagger
                .antMatchers("/swagger**/**").permitAll()
                .antMatchers("/webjars/**").permitAll()
                .antMatchers("/v2/**").permitAll()
                .antMatchers("/thymeleaf/**").permitAll();
            //    .anyRequest().authenticated();

        http.formLogin()
                //.loginPage("/login")//默认配置,如果此处指定则默认页面不起作用
                .loginProcessingUrl("/doLogin")//默认配置
                .usernameParameter("username")//默认配置
                .passwordParameter("password")//默认配置
                .failureHandler(authenticationFailureHandler)//自定义认证失败处理器
                .successHandler(customAuthenticationSucessHandler)//自定义成功处理
               .defaultSuccessUrl("/demo/demo01", true);//登录成功后默认跳转页面
    }

    @Override
    protected void configure(AuthenticationManagerBuilder auth) throws Exception {

        //内存认证服务配置
        auth.inMemoryAuthentication()
                .passwordEncoder(new BCryptPasswordEncoder())
                .withUser("imadmin").password(new BCryptPasswordEncoder().encode("imadmin123")).roles("vip1")
                .and()
                .withUser("imdemo").password(new BCryptPasswordEncoder().encode("imdemo123")).roles("demo1");

        //自定义认证服务配置
        auth.userDetailsService(userDetailsServiceImpl).passwordEncoder(new BCryptPasswordEncoder());

    }
}
